<?php
require 'AES.php';
class Ext_Auth_Adapter_Passport implements Zend_Auth_Adapter_Interface
{
	protected $_username;
	protected $_password;
	protected $_productId = 2;
	protected $_url = "http://itc.m2.com.cn:8080/itcpassport/index.php?c=xml&ac=interface&receive=";
	protected $_key = "12few5fs6sdit6afnt43nd4tsnfwdns4";
	protected $_returnObject = NULL;
	
	public function __construct($username, $password, $productId = null, $url=null, $key=null)
	{
		$this->setUsername($username);
		$this->setPassword($password);
		if (null !== $productId) {
			$this->setPassword($password);			
		}
		if (null !== $url) {
			$this->setUrl($url);
		}
		if (null !== $key) {
			$this->setKey($key);
		}
	}
	
	protected function createApiUrl()
	{
		$pwd = md5($this->getPassword());
		$account = $this->getUsername();
		$content='ac='.$account.',pwd='.$pwd.',tm='.time();
		$key = $this->getKey();
		$Cipher=new Ext_AES($key);
		$en=$Cipher->encrypt($content);
		$en=URLencode($en);
		$en=$en."&product=".$this->getProductId();	
		
		$url = $this->getUrl().$en;
		
		return $url;
	}
	
	/**
	 * @return the $_username
	 */
	public function getUsername() {
		return $this->_username;
	}

	/**
	 * @return the $_password
	 */
	public function getPassword() {
		return $this->_password;
	}

	/**
	 * @return the $_productId
	 */
	public function getProductId() {
		return $this->_productId;
	}

	/**
	 * @return the $_url
	 */
	public function getUrl() {
		return $this->_url;
	}

	/**
	 * @return the $_key
	 */
	public function getKey() {
		return $this->_key;
	}

	/**
	 * @param field_type $_username
	 */
	public function setUsername($_username) {
		$this->_username = $_username;
	}

	/**
	 * @param field_type $_password
	 */
	public function setPassword($_password) {
		$this->_password = $_password;
	}

	/**
	 * @param field_type $_productId
	 */
	public function setProductId($_productId) {
		$this->_productId = $_productId;
	}

	/**
	 * @param field_type $_url
	 */
	public function setUrl($_url) {
		$this->_url = $_url;
	}

	/**
	 * @param field_type $_key
	 */
	public function setKey($_key) {
		$this->_key = $_key;
	}

	public function authenticate()
	{
		$url = $this->createApiUrl();
		
		$output = file_get_contents($url);
		$doc = new DOMDocument();
		$doc->loadXML($output);
		$errs = $doc->getElementsByTagName('error');		
		if ($errs->length > 0)
		{			
			return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, $this->getUsername());
		}
		else 
		{			
			$uid = $doc->getElementsByTagName('uid')->item(0)->nodeValue;
			$name = $doc->getElementsByTagName('name')->item(0)->nodeValue;
			$mobile = $doc->getElementsByTagName('mobile')->item(0)->nodeValue;
			$company = $doc->getElementsByTagName('company')->item(0)->nodeValue;
			$role_id = $doc->getElementsByTagName('role_id')->item(0)->nodeValue;
			
			$this->_returnObject = new stdClass();
			$this->_returnObject->{'uid'} = $uid;
			$this->_returnObject->{'name'} = $name;
			$this->_returnObject->{'mobile'} = $mobile;
			$this->_returnObject->{'company'} = $company;
			$this->_returnObject->{'role_id'} = $role_id;
			return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $this->getUsername());
		}		
		
	}
	
	public function getResultRowObject()
	{
		return $this->_returnObject;
	}
}
?>